package com.tepth.remote.control.delegate.picture.photo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.tepth.remote.control.R;
import com.tepth.remote.control.R2;
import com.tepth.remote.control.delegate.BasePermissionCheckerDelegate;

import butterknife.BindView;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/23
 */
public class PhotoDelegate extends BasePermissionCheckerDelegate {

    private static final String URL = "url";
    /**
     * 设置图片加载策略
     */
    private static final RequestOptions REQUEST_OPTIONS = new RequestOptions()
            .centerCrop()
            .error(R.drawable.image_error)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .dontAnimate();

    @BindView(R2.id.photo_view)
    PhotoView mPhotoView;

    public static PhotoDelegate create(String url) {
        final PhotoDelegate contentDelegate = new PhotoDelegate();
        final Bundle bundle = new Bundle();
        bundle.putString(URL, url);
        contentDelegate.setArguments(bundle);
        return contentDelegate;
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_photo;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        final Bundle bundle = getArguments();
        mPhotoView.setOnPhotoTapListener(new OnPhotoTapListener() {
            @Override
            public void onPhotoTap(ImageView view, float x, float y) {
                pop();
            }
        });
        final String url;
        if (bundle != null) {
            url = bundle.getString(URL);
        } else {
            url = "";
        }
        Glide.with(getProxyActivity())
                .load(url)
                .apply(REQUEST_OPTIONS)
                .into(mPhotoView);
    }
}
